package admin

import (
	"duoke/context"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/request"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"net/http"
	"net/url"
)

var AccountSetting = new(accountSettingCtl)

type accountSettingCtl struct{}

// 获取设置信息
func (ctl *accountSettingCtl) Info(c *gin.Context) {
	req := request.JsonMap(c)
	if req["field"] == "" {
		response.Error(c, "field缺失", nil)
		return
	}
	info, err := service.AccountSetting.Info(c, gconv.String(req["field"]))
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", info)
}

// 设置保存
func (ctl *accountSettingCtl) Save(c *gin.Context) {
	req := request.JsonMap(c)
	if req["field"] == "" || req["data"] == "" {
		response.Error(c, "field/data数据缺失", nil)
		return
	}
	err := service.AccountSetting.Save(c, req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "保存成功", nil)
}

// 上传小程序代码
func (ctl *accountSettingCtl) UploadMinicode(c *gin.Context) {
	accountR, _ := service.Request.Account(c)
	setting, err := service.AccountSetting.Info(c, "miniprogram")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if gconv.String(setting["app_id"]) == "" {
		response.Error(c, "请先设置AppId并保存", nil)
		return
	}
	if gconv.String(setting["private_key"]) == "" {
		response.Error(c, "请先设置AppSecret并保存", nil)
		return
	}
	if gconv.String(setting["version"]) == "" {
		response.Error(c, "请先设置小程序版本号并保存", nil)
		return
	}
	postData := make(url.Values)
	postData["appname"] = []string{"supertuan"}
	postData["appid"] = []string{gconv.String(setting["app_id"])}
	postData["privatekey"] = []string{gconv.String(setting["private_key"])}
	postData["acid"] = []string{gconv.String(accountR.Id)}
	postData["apiurl"] = []string{gconv.String(context.Global.Domain)}
	postData["version"] = []string{gconv.String(setting["version"])}
	// 构建包
	resp, err := http.PostForm("http://support.doying.cc/minici/build/", postData)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	result := string(body)
	code := gjson.Get(result, "code")
	if !code.Exists() {
		response.Error(c, "上传服务通信出错", nil)
		return
	}
	msg := gjson.Get(result, "msg")
	if gconv.String(code.Value()) == "0" {
		response.Error(c, gconv.String(msg.Value()), nil)
		return
	}
	// 执行上传
	resp, err = http.PostForm("http://support.doying.cc/minici/upload/", postData)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	defer resp.Body.Close()
	body, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	result = string(body)
	code = gjson.Get(result, "code")
	if !code.Exists() {
		response.Error(c, "上传服务通信出错", nil)
		return
	}
	msg = gjson.Get(result, "msg")
	if gconv.String(code.Value()) == "0" {
		response.Error(c, gconv.String(msg.Value()), nil)
		return
	}
	response.Success(c, result, nil)
}
